#-------------------------------------------------------------------------------
# Name:        Console
# Purpose:
#
# Author:      Michael Blackwood
#
# Created:     18/11/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, logging

import animatedsprite

class Console(animatedsprite.AnimatedSprite):

  """ Ship computer consoles."""

  def __init__(self, objtype, name, attributes, imagehandler):
    animatedsprite.AnimatedSprite.__init__(self, objtype, name, attributes, imagehandler)
    self.ancestors.append('console')
    rect = self.attributes['activaterect'].split(',')
    rect = (int(rect[0]), int(rect[1]))
    self.attributes['activaterect'] = rect
    self.attributes['beingused'] = False
    self.attributes['playerinrange'] = False
    self.attributes['required'] = False
    self.attributes['result'] = None
    self.attributes['emergency'] = self.attributes['emergency'] == 'True'


  def update(self, common, player):

    """ Update the console """

    # Animate the console
    self._animupdate(common)

    # Update the console
    if self.attributes['visable']:
      self._consoleupdate(common, player)


  def reset(self):

    """ Resets the console after use. I BROKE SOMETHING AT ONE POINT SO ALWAYS USE THIS,
        IT /SHOULD/ WORK. #PABLOSERIES """

    self.attributes['beingused'] = False
    self.attributes['required'] = False
    self.attributes['result'] = None

  def _consoleupdate(self, common, player):

    """ Update console-related specifics """

    activaterect = self.getActivateRect()

    playerpos = player.getRect().center

    self.attributes['beingused'] = False
    self.attributes['playerinrange'] = False

    if activaterect.collidepoint(playerpos):
      self.attributes['playerinrange'] = True

      if player.attributes['use'] and self.attributes['required']:
        logging.debug('Activating console "%s', self.name)
        common.debug.add('Activating console ' + self.name)
        self.attributes['beingused'] = True
        self.attributes['required'] = False



  def getActivateRect(self):

    """ Get the rect the player needs to be within to use console """
    bounding = self.getBoundingRect()
    rect = pygame.Rect((0,0), self.attributes['activaterect'])
    rect.center = bounding.center
    return rect


